Keyboard shortcuts

Press ← or → to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

13.2 Early Option Parsing

For ARM systems, the primary function of parsing early kernel parameters is to configure specific attributes for the early console (earlyconsole). The function responsible for executing this early argument parsing is parse_early_param(), which is decorated with the __init attribute and defined within the source file git/init/main.c. Its definition is implemented as follows:

void __init parse_early_param(void)
{
	static int done __initdata;
	static char tmp_cmdline[COMMAND_LINE_SIZE] __initdata;
	if (done)
		return;
	strlcpy(tmp_cmdline, boot_command_line, COMMAND_LINE_SIZE);
	parse_early_options(tmp_cmdline);
	done = 1;
}

After copying the command-line arguments from boot_command_line into a temporary staging buffer, parse_early_param() invokes the function parse_early_options() to process the text string. This function acts as a wrapper around the generalized parse_args() routine and is declared inside git/init/main.c as follows:

void __init parse_early_options(char *cmdline)
{
	parse_args("early options", cmdline, NULL, 0, 0, 0, NULL, do_early_param);
}

Within this scope, "early options" represents the descriptive target identity used to print verbose debugging traces. cmdline points to the raw command-line string payload targeted for parsing. The callback function do_early_param is specifically provided to handle variables that the core kernel parameter registry cannot natively identify.

The definition of the core parse_args() routine is hosted within the source file git/kernel/params.c and is implemented as follows:

char *parse_args(const char *doing, 
                char *args, 
                const struct kernel_param *params, 
                unsigned num, 
                s16 min_level, 
                s16 max_level, 
                void *arg, 
                int (*unknown)(char *param, char *val, const char *doing, void *arg)) 
{
	char *param, *val, *err = NULL;
	args = skip_spaces(args);
	if (*args)
		pr_debug("doing %s, parsing ARGS: '%s'\n", doing, args);
	while (*args) {
		int ret;
		int irq_was_disabled;
		args = next_arg(args, &param, &val);	
		if (!val && strcmp(param, "--") == 0)	
			return err ?: args;
		irq_was_disabled = irqs_disabled();
		ret = parse_one(param, val, doing, params, num, min_level, max_level, arg, unknown);
		if (irq_was_disabled && !irqs_disabled())
			pr_warn("%s: option '%s' enabled irq's!\n", doing, param);
		switch (ret) {
		case 0:
			continue;
		case -ENOENT:
			pr_err("%s: Unknown parameter `%s'\n", doing, param);
			break;
		case -ENOSPC:
			pr_err("%s: `%s' too large for parameter `%s'\n", doing, val ?: "", param);
			break;
		default:
			pr_err("%s: `%s' invalid for parameter `%s'\n", doing, val ?: "", param);
			break;
		}
		err = ERR_PTR(ret);
	}
	return err;
}

The function next_arg(args, &param, &val) scans the string payload referenced by args (the cmd_line data) to isolate the position of the next parameter group, and returns a pointer tracking that next block. param captures the target address of the variable currently being parsed. val points to the specific parameter value corresponding to the current variable—meaning the text string segment trailing the = delimiter. If the assignment delimiter = cannot be located, the value defaults to a null pointer. next_arg() isolates the boundaries of the active variable group and the succeeding token block by parsing special control characters such as ", -, --, and = inside the cmd_line string. It strips out leading and trailing whitespaces enclosing the parameter group and overwrites the literal characters " and = with null terminators ('\0'). The function next_arg() is defined inside the file git/lib/cmdline.c.

The function parse_one(param, val, doing, params, num, min_level, max_level, arg, unknown) handles the parsing logic for a single variable pair. param represents a standalone variable extracted from the command line, while val references the corresponding text value assigned to that variable. doing is a text string marking the active operational phase, which is primarily leveraged to print debugging traces. params represents an array composed of elements of type struct kernel_param, which enumerates the valid variables that this routine is authorized to handle. Any command-line arguments that fall outside the explicit boundary constraints specified within the params registry are handed off to the unknown function callback (provided as the final argument input) for customized processing. The definition of the kernel_param structure is implemented as follows:

struct kernel_param {
	const char *name;
	struct module *mod;
	const struct kernel_param_ops *ops;
	const u16 perm;
	s8 level;
	u8 flags;
	union {
		void *arg;
		const struct kparam_string *str;
		const struct kparam_array *arr;
	};
};

This structure outlines the parameter name, the specific kernel module ownership mapping, and the operational callback table used to set and get the underlying value. The perm field determines whether the module parameter is exposed visually within the sysfs filesystem infrastructure. The level field dictates the execution priority level of the module during the initcall stabilization passes. The generalized concept of initcall execution tracks will be analyzed across subsequent chapters. The flags field configures the safety and security attributes of the parameter, where bit 0 indicates whether setting this parameter will taint the active kernel instance, and bit 1 establishes whether it is permissible to configure this parameter under hardware lockdown constraints. These two specific bit definitions correspond to:

enum {
	KERNEL_PARAM_FL_UNSAFE    = (1 << 0),
	KERNEL_PARAM_FL_HWPARAM   = (1 << 1),
};

The routines utilized to assign and read the parameter parameters are grouped inside the kernel_param_ops structure, which is structured as follows:

struct kernel_param_ops {
	unsigned int flags;	
	int (*set)(const char *val, const struct kernel_param *kp);
	int (*get)(char *buffer, const struct kernel_param *kp);
	void (*free)(void *arg);
};

Within this layout, the flags field indicates whether a null or empty value configuration is valid for this specific variable. The available bitmask flag resolves to:

enum {
	KERNEL_PARAM_OPS_FL_NOARG = (1 << 0)
}; 

The set field points to the callback routine used to configure the variable value, while the get field points to the callback routine used to read back the active variable value.

The function parse_one() is declared as follows:

static int parse_one(char *param, 
    char *val, 
    const char *doing, 
    const struct kernel_param *params, 
    unsigned num_params, 
    s16 min_level, 
    s16 max_level, 
    void *arg, 
    int (*handle_unknown)(char *param, char *val, const char *doing, void *arg))
{
	unsigned int i;
	int err;
	for (i = 0; i < num_params; i++) {
		if (parameq(param, params[i].name)) {
			if (params[i].level < min_level || params[i].level > max_level)
				return 0;	
			if (!val && !(params[i].ops->flags & 
   KERNEL_PARAM_OPS_FL_NOARG))
				return -EINVAL;
			pr_debug("handling %s with %p\n", param, params[i].ops->set);
			kernel_param_lock(params[i].mod);
			if (param_check_unsafe(&params[i]))
				err = params[i].ops->set(val, &params[i]);
			else
				err = -EPERM;
			kernel_param_unlock(params[i].mod);
			return err;
		}
	}
	if (handle_unknown) {
		pr_debug("doing %s: %s='%s'\n", doing, param, val);
		return handle_unknown(param, val, doing, arg);
	}
	pr_debug("Unknown argument '%s'\n", param);
	return -ENOENT;
}

The parse_one() function is defined within the source file git/kernel/params.c.

parse_one() initially verifies whether the specific parameter designated by param is contained within the variable registry specified by params. If the argument falls outside this registered scope, param is handed off to the handle_unknown callback function (which maps to do_early_param). Conversely, if the argument is found within the specified registry, parse_one() determines whether the initcall level of the variable falls within the boundaries bounded by the maximum and minimum levels. If the priority constraints are satisfied, the function proceeds to check whether the flags field of this parameter evaluates to KERNEL_PARAM_FL_UNSAFE. Provided that the configuration does not negatively compromise kernel runtime safety, it further evaluates whether the variable value is a null value. In the event of a null value configuration, the routine checks whether the parameter's operational flags permit an empty value assignment. Upon passing all validation blocks, parse_one() invokes the customized set callback provided by the variable itself to apply the parameter value. The initcall tier establishes the exact scheduling and execution sequence of a given initialization routine. The internal execution topology of these initialization layers will be comprehensively analyzed when introducing the underlying initcall operational mechanics.

For ARM architectures, the specific manifestation of invoking the parse_args() function is executed as:

    parse_args("early options", cmdline, NULL, 0, 0, 0, NULL, do_early_param)

Because this explicit invocation supplies no structural array enumerating the targeted variables to be parsed, the parse_one() function directly triggers the do_early_param handler callback to process the incoming param variable. The do_early_param function is declared within the file git/init/main.c, and its definition is as follows:

static int __init do_early_param(char *param, char *val, const char *unused, void *arg)
{
	const struct obs_kernel_param *p;
	for (p = __setup_start; p < __setup_end; p++) {
		if ((p->early && parameq(param, p->str)) || (strcmp(param, "console") == 0
 		    && strcmp(p->str, "earlycon") == 0)) {
			if (p->setup_func(val) != 0)
				pr_warn("Malformed early option '%s'\n", param);
		}
	}
	return 0;
}

The structure definition for obs_kernel_param is implemented as:

struct obs_kernel_param {
	const char *str;
	int (*setup_func)(char *);
	int early;
};

The do_early_param function first evaluates whether the memory segment bounded precisely between __setup_start and __setup_end encloses a tracking descriptor matching the argument passed via param. It checks whether this descriptor explicitly enables the early attribute flag, or whether the param string matches "console" while the __setup_start to __setup_end table contains the "earlycon" tracking signature. If either condition is structurally satisfied, it invokes the corresponding parameter initialization callback setup_func() to initialize the configuration targeted by param.

As established from the preceding layout description, the function parse_early_param() executes no operations on parameters it cannot structurally identify, deliberately leaving those unparsed tokens to be resolved by subsequent backend processing stages.

If an exception or parsing failure occurs throughout the tokenization of a given variable pair, the parse_args() function immediately terminates the evaluation of any remaining command-line components, translates the resulting error code (situated between -1 and -4095) into a pointer pointing to a void type, and returns it. If the entire string payload is successfully processed without errors, it returns the precise memory address pointing to the double-hyphen delimiter "──" within the command line.

The complete function execution call graph for parsing kernel command-line parameters during the early system boot phase is illustrated in Figure 13-1.

Figure 13-1 Parameter Parsing Workflow during Early Boot Phase

Copyright © 2026 Guangzhou eSmart Information Technology Co., Ltd. All Rights Reserved.
All content in this book is copyrighted by the author. For personal learning and exchange only. Commercial use without authorization is strictly prohibited. Email:

✉ 3348194517@qq.com | hai616878@gmail.com